Ext.regModel('Ext.ux.mattgoldspink.subsonic.model.Menu', {
    idProperty: 'id',
    fields: ['id', 'name', 'iconCls'] 
});

Ext.ux.mattgoldspink.LeftMenuStore = Ext.extend(Ext.data.Store, {
    constructor: function(config){
		var params = {};
		Ext.apply(params, Ext.ux.mattgoldspink.subsonic.LoginDetails);
		Ext.apply(config, {
            model: 'Ext.ux.mattgoldspink.subsonic.model.Menu',
            id:'Ext.ux.mattgoldspink.subsonic.stores.LeftMenuStore',
			sorters: [
				{
					property: 'iconCls',
					direction: 'ASC'
				}, {
					property: 'name',
					direction: 'ASC'
				}],
            proxy: {
                type: 'ajax',
                url: Ext.ux.mattgoldspink.subsonic.apiUrl + '/rest/getPlaylists.view',
                extraParams: params,
                requestMethod: 'GET',
                reader: this.makePlaylistReader()
            }
        });
		this.task = new Ext.util.DelayedTask(this.initLogin, this);
		this.task.delay(50);
        Ext.ux.mattgoldspink.LeftMenuStore.superclass.constructor.call(this, config);
    },
	initLogin: function(){
		if (!Ext.isEmpty(Ext.ux.mattgoldspink.subsonic.apiUrl)) {
			this.getProxy().url = Ext.ux.mattgoldspink.subsonic.apiUrl + '/rest/getPlaylists.view';
		}
		if (!Ext.isEmpty(Ext.ux.mattgoldspink.subsonic.LoginDetails.u) && !Ext.isEmpty(Ext.ux.mattgoldspink.subsonic.LoginDetails.p)){
			Ext.apply(this.getProxy().extraParams, Ext.ux.mattgoldspink.subsonic.LoginDetails);
			this.load();
			delete this.task;
		} else {
			this.task.delay(50);
		}
	},
	load: function(options) {
		// do a call to get the MusicFolders too
		if (Ext.isEmpty(options)) {
			options = {};
		}
		options.callback = this.loadMusicFolders;
		options.scope = this;
		return Ext.ux.mattgoldspink.LeftMenuStore.superclass.load.call(this, options);
	},
	loadMusicFolders: function(options) {
		var params = Ext.apply({}, Ext.ux.mattgoldspink.subsonic.LoginDetails);
		Ext.Ajax.request({
            url: Ext.ux.mattgoldspink.subsonic.apiUrl + '/rest/getMusicFolders.view',
            scope: this,
			params: params,
			success: function(data) {
			    data = Ext.decode(data.responseText);
                var status = data['subsonic-response'].status;
                if (status === 'ok') {
					var recs;
                    data = data['subsonic-response'];
					recs = data.musicFolders.musicFolder;
                    if (!Ext.isArray(recs)) {
                        data.musicFolders.musicFolder = [recs];
                    }
                    Ext.each(recs, function(rec){
                        rec.typeOfRequest = 'musicFolder';
						rec.iconCls = 'music';
                    });
					this.loadData(data.musicFolders.musicFolder, true);
				}
			}
		});
	},
    makePlaylistReader: function(){
        if (!Ext.isDefined(this.playlistReader)) {
            this.playlistReader = new Ext.data.JsonReader({
                root: 'playlists.playlist',
                getData: function(data){
                    var recs;
                    data = data['subsonic-response'];
					recs = data.playlists.playlist;
					if (!Ext.isArray(recs)) {
						recs = [recs];
					}
					data.playlists.playlist = recs;
					Ext.each(data.playlists.playlist, function(playlist) {
						if (!playlist.iconCls) {
							playlist.iconCls = 'playlist';
						}
					});
					return data;
                }
            });
        }
        return this.playlistReader;
    },
    makeMusicFolderReader: function(){
        if (!Ext.isDefined(this.musicFolderReader)) {
            this.musicFolderReader = new Ext.data.JsonReader({
                root: 'musicFolders.musicFolder',
                getData: function(data){
                    var recs;
                    data = data['subsonic-response'];
                    recs = data.musicFolders.musicFolder;
                    if (!Ext.isArray(recs)) {
                        data.musicFolders.musicFolder = [recs];
                    }
                    Ext.each(recs, function(rec){
                        rec.typeOfRequest = 'musicFolder';
                    });
                    return data;
                }
            });
        }
        return this.musicFolderReader;
    } 
});
